﻿using System.Diagnostics;

namespace _FFMPEG队列压缩测试
{
    internal class Program
    {
        static void Main(string[] args)
        {
            for (int i = 1; i < 200; i++)
            {
                var res = DivideIntoThreeParts(i);
                Console.WriteLine($"{i}:"+res.Item1 + "," + res.Item2 + "," + res.Item3);

                Console.WriteLine(  );
            }
          
        }

        public static (int, int, int) DivideIntoThreeParts(int total)
        {
            // 初始化第一份
            int firstPart;

            if (total >= 4)
            {
                // 计算大致的平均值
                int average = total / 3;
                // 向上找最近的4的倍数
                firstPart = ((average + 3) / 4) * 4;

                // 如果第一份过大，就往下取一个4的倍数
                if (firstPart > average + 2)
                {
                    firstPart = ((average - 1) / 4) * 4;
                }

                // 确保至少为4
                firstPart = Math.Max(4, firstPart);
            }
            else
            {
                firstPart = 0;
            }

            // 计算剩余金额
            int remaining = total - firstPart;

            // 将剩余金额平均分配
            int thirdPart = remaining / 2;
            int secondPart = remaining - thirdPart;

            return (firstPart, secondPart, thirdPart);
        }
        private static void ExcuteCMD(string cmd, string outFile)
        {
            try
            {
                Console.WriteLine("执行ffpeg压缩指令");
                Process p = new Process();
                p.StartInfo.FileName = $"{System.AppDomain.CurrentDomain.BaseDirectory}ffmpeg.exe";
                Console.WriteLine($"{System.AppDomain.CurrentDomain.BaseDirectory}ffmpeg.exe");
                p.StartInfo.Arguments = cmd;
                p.Exited += async (sender, e) =>
                {

                };

                p.ErrorDataReceived += P_ErrorDataReceived;
                p.StartInfo.UseShellExecute = false;
                p.StartInfo.CreateNoWindow = true; // 不显示 DOS 程序窗口
                p.StartInfo.RedirectStandardInput = true;
                p.StartInfo.RedirectStandardOutput = true;
                p.StartInfo.RedirectStandardError = true;
                p.Start();
                p.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
                p.BeginErrorReadLine();
                p.WaitForExit();
                int pcode = p.ExitCode;
                Console.WriteLine("ExitCode:" + pcode);
                if (pcode != 0)
                {
                    Console.WriteLine("ExitCode:" + pcode, null);
                }
                else
                {
                    Console.WriteLine("ExitCode:" + pcode);
                }
                Console.WriteLine("执行ffpeg压缩指令[WaitForExit]");
                p.Close();//关闭进程
                p.Dispose();//释放资源
                Console.WriteLine("释放ffpeg压缩指令");
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message, ex);
            }

        }

        private static void P_ErrorDataReceived(object sender, DataReceivedEventArgs e)
        {

        }
    }
}
